﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace PageOffice6_Net_Core.Controllers.FileMaker
{
    public class FileMakerController : Controller
    {
        private readonly IWebHostEnvironment _webHostEnvironment;

        public FileMakerController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }
        public IActionResult Index()
        {
            string url = "";
            url = _webHostEnvironment.WebRootPath;
            ViewBag.url = url + "\\FileMaker\\doc";
            return View();
        }
        public void FileMaker(int id)
        {
            PageOfficeNetCore.FileMakerCtrl fileMakerCtrl = new PageOfficeNetCore.FileMakerCtrl(Request);

            PageOfficeNetCore.Word.WordDocumentWriter doc = new PageOfficeNetCore.Word.WordDocumentWriter();
   
            //给数据区域赋值，即把数据填充到模板中相应的位置
            doc.OpenDataRegion("PO_company").Value = "北京卓正志远软件有限公司  " + id;
            fileMakerCtrl.SetWriter(doc);
 
            //打开文档
            fileMakerCtrl.FillDocument("doc/template.doc", PageOfficeNetCore.DocumentOpenType.Word);
            Response.Body.WriteAsync(Encoding.GetEncoding("GB2312").GetBytes(fileMakerCtrl.GetHtml()));
        }

        public async Task<ActionResult> SaveDoc(int id)
        {
            PageOfficeNetCore.FileSaver fs = new PageOfficeNetCore.FileSaver(Request, Response);
            await fs.LoadAsync();
            string webRootPath = _webHostEnvironment.WebRootPath;
            string fileName = "maker" + id + fs.FileExtName; ;
            fs.SaveToFile(webRootPath + "/FileMaker/doc/" + fileName);
            return fs.Close();
        }
    }
}